// Ideology/partisanship scales
log using IdeologyANES24pilot.log, replace
use "C:\Users\sbstjp\OneDrive - Cardiff University\anes_pilot_2024_dta_20240319.dta" // American National Election Study 2024 Pilot Study, March 19, 2024 Version. Date accessed: March 09, 2025.

keep if sample_type == 1 // Only keep those respondents with proper weight, as advised in codebook

// Delete missing values from ideology scale
replace ideology_lcself= . if inlist(ideology_lcself, -1, -7)

// Reverse ideology so liberalism coded high
egen maxval = max(ideology_lcself)
gen Rideology_lcself = maxval + 1 - ideology_lcself

// Delete missing values from Democratic Party thermometer
replace party_democ=. if party_democ==999 
replace party_democ=. if party_democ==-7

// Create liberalism scale
* Delete missing values
local vars abrtpre_standard resent_deserve

foreach var in `vars' {
    replace `var' = . if `var' < 0
}

replace abrtpre_standard=. if abrtpre_standard==5 // about 5% chose other

*Reverse coding so liberal are high
foreach var in resent_genertns resent_deserve {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in treatcrime_marij border_milit abrtpre_standard rresent_genertns rresent_deserve sexism_hinnoc sexism_hpower {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

// Deal with missing values and create the scale
* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values
foreach var in streatcrime_marij sborder_milit sabrtpre_standard ssexism_hinnoc ssexism_hpower srresent_genertns srresent_deserve {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreAL = 0
gen count_nonzeroAL = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in streatcrime_marij sborder_milit sabrtpre_standard ssexism_hinnoc ssexism_hpower srresent_genertns srresent_deserve {
    replace total_scoreAL = total_scoreAL + `var'
    replace count_nonzeroAL = count_nonzeroAL + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen LibValues = . 
replace LibValues = total_scoreAL / count_nonzeroAL if count_nonzeroAL > 0

//Create social justice scale
*Delete missing values
replace group_antifa= . if inlist(group_antifa, -7, 999)

*Generate a dummy variable for raceadvantage
gen raceadwhite=. 
replace raceadwhite=1 if raceadvt_white==3 & raceadvt_whitestr==1
replace raceadwhite=2 if raceadvt_white==3 & raceadvt_whitestr==2
replace raceadwhite=3 if raceadvt_white==3 & raceadvt_whitestr==3
replace raceadwhite=4 if raceadvt_white==2
replace raceadwhite=5 if raceadvt_white==1 & raceadvt_whitestr==3
replace raceadwhite=6 if raceadvt_white==1 & raceadvt_whitestr==2
replace raceadwhite=7 if raceadvt_white==1 & raceadvt_whitestr==1

*Reverse code certain items so social justice values are high
foreach var in police_number trans_health school_gender {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in rschool_gender rtrans_health rpolice_number raceadwhite group_blm group_antifa {
    summarize `var'
    gen s_`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values and means a 0-1 standardization scale isn't feasible as missing values would overlap with the scale
foreach var in s_rschool_gender s_rtrans_health s_rpolice_number s_raceadwhite s_group_blm s_group_antifa {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_score = 0
gen count_nonzero = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in s_rschool_gender s_rtrans_health s_rpolice_number s_raceadwhite s_group_blm s_group_antifa {
    replace total_score = total_score + `var'
    replace count_nonzero = count_nonzero + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen SocJusValues = . 
replace SocJusValues = total_score / count_nonzero if count_nonzero > 0

// Correlations
pwcorr SocJusValues LibValues Rideology_lcself party_democ [aweight=weight], sig

// Ideological constraint
*Create graduate dummy
gen Graduate=. 
replace Graduate=0 if inrange(educ, 1, 4)
replace Graduate=1 if inrange(educ, 5, 6)

// Correlations
pwcorr rschool_gender rtrans_health rpolice_number raceadwhite group_blm group_antifa [aweight=weight], sig
pwcorr rschool_gender rtrans_health rpolice_number raceadwhite group_blm group_antifa if newsint==1 [aweight=weight], sig 
pwcorr rschool_gender rtrans_health rpolice_number raceadwhite group_blm group_antifa if Graduate==1 [aweight=weight], sig 
pwcorr rschool_gender rtrans_health rpolice_number raceadwhite group_blm group_antifa if Rideology_lcself>4 [aweight=weight], sig 
pwcorr rschool_gender rtrans_health rpolice_number raceadwhite group_blm group_antifa if pid7==1 [aweight=weight], sig 

log close
